//	Evaluator.java

import java.io.*;

/** 
	Description: CS 354

	@author Teresa Cole
 *     @author Samuel Jenkins
	@version Fall 2003 - modified Fall 2008
*/
public class Evaluator implements Token
{
    /** Evaluate a parse tree

    @param pt is a Lexeme which is the root of the parse tree
    @return Lexeme containing result of evaluation (should be a
    NUMBER Lexeme)
    */
    public Lexeme eval( Lexeme pt)
    {
    Lexeme answer = null, lhs, rhs, rand, rand1, rand2;

    switch (pt.getType())
	{
	case VAR:
	    answer = new Lexeme( 0);
	    break;
	case ASSIGN:
	    answer = eval( pt.getRight());
	    break;
	case NUMBER:
	    answer = pt;
	    break;
	case PLUS:
	    lhs = eval( pt.getLeft());
	    rhs = eval( pt.getRight());
	    answer = new Lexeme( NUMBER, lhs.getValue() + rhs.getValue());
	    break;
	case MINUS:
	    lhs = eval( pt.getLeft());
	    rhs = eval( pt.getRight());
	    answer = new Lexeme( NUMBER, lhs.getValue() - rhs.getValue());
	    break;
	case TIMES:
	    lhs = eval( pt.getLeft());
	    rhs = eval( pt.getRight());
	    answer = new Lexeme( NUMBER, lhs.getValue() * rhs.getValue());
	    break;
	case DIVIDE:
	    lhs = eval( pt.getLeft());
	    rhs = eval( pt.getRight());
	    answer = new Lexeme( NUMBER, lhs.getValue() / rhs.getValue());
	    break;
	default:

	}
//    System.out.println( "\t returning " + answer);
    return answer;
    }
}
